/*!
\file InputWidget.cpp
\brief Contains implementation of InputWidget
*/

#include"InputWidget.h" 

InputWidget::InputWidget( Graph *g, QString comment ):QDialog( 0 ), m_graph(g){
	m_selectedID = 0;
	
	m_outerLayout = new QVBoxLayout(this);
	setLayout(m_outerLayout);
	QLabel *label = new QLabel(comment, this);
	m_outerLayout->addWidget(label);
	
	m_outer = new QWidget(this);
	m_outerLayout->addWidget(m_outer);
	m_layout = new QHBoxLayout(m_outer);
	m_outer->setLayout( m_layout );
	
	DManager *manager = new DManager( g->toDOT() );
	m_label = new QLabel(m_outer);
	m_label->setPixmap(manager->toQPixmap());
	m_layout->addWidget(m_label);
	
	m_buttonGroup = new QButtonGroup(m_outer);
	m_buttonGroup->setExclusive(true);
	connect( m_buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(selectionChanged(int)) );
	
	m_widget = new QWidget(m_outer);
	m_layout->addWidget(m_widget);
	
	m_childLayout = new QVBoxLayout(m_widget);
	m_widget->setLayout(m_childLayout);
	
	delete manager;
	
	m_dialogButtons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
	m_outerLayout->addWidget(m_dialogButtons);
	connect(m_dialogButtons, SIGNAL(accepted()), this, SLOT(accept()));
	connect(m_dialogButtons, SIGNAL(rejected()), this, SLOT(reject()));
}

InputWidget::~InputWidget(){

}

Node *InputWidget::resultNode(){
	return new Node(m_graph->node(m_selectedID));
}

Edge *InputWidget::resultEdge(){
	return new Edge(m_graph->edge(m_selectedID));
}

void InputWidget::showEdges(){
	for( unsigned int i = 0; i < m_graph->numEdges(); i++ ){
		QCheckBox *checkBox = new QCheckBox( m_graph->edge(i).toString(), m_widget );
		if(i == 0)
			checkBox->setChecked(true);
		m_childLayout->addWidget(checkBox);
		m_buttonGroup->addButton(checkBox, i);
	}
}

void InputWidget::showNodes(){
	for( unsigned int i = 0; i < m_graph->numNodes(); i++ ){
		QCheckBox *checkBox = new QCheckBox( m_graph->node(i).toString(), m_widget );
		if(i == 0)
			checkBox->setChecked(true);
		m_childLayout->addWidget(checkBox);
		m_buttonGroup->addButton(checkBox, i);
	}
}

Node *InputWidget::getNode( Graph *g, QString comment ){
	InputWidget *input = new InputWidget(g, comment);
	input->showNodes();
	
	Node* ret = 0;
	if(input->exec()){
		ret = input->resultNode();
	}
	
	delete input;
	return ret;
}

Edge *InputWidget::getEdge( Graph *g, QString comment ){
	InputWidget *input = new InputWidget(g, comment);
	input->showEdges();
	
	Edge *ret = 0;
	if(input->exec()){
		ret = input->resultEdge();
	}
	
	delete input;
	return ret;
}

void InputWidget::selectionChanged(int selected){
	m_selectedID = selected;
}
